/**
 * @Description MyAddressController.java
 * @author xu_cc [18724000100@163.com]
 * @date 创建时间：2017年12月5日 上午9:08:15
 */
package com.solution.pc.my.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.DeliveryAddressEntity;
import com.solution.admin.customer.service.DeliveryAddressService;
import com.solution.admin.point.constants.PointConstants.IsYesNo;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 我的收货地址
 * 
 * @author xu_cc
 * @date 创建时间：2017年12月4日 下午7:34:28
 */
@RequestMapping("/pc/my/address")
@Controller("myAddressController")
public class MyAddressController extends BaseController {

	@Autowired
	DeliveryAddressService addressService;

	/**
	 * 收货地址
	 * 
	 * @param request
	 * @param model
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年12月4日 上午11:31:31
	 */
	@RequestMapping("")
	public String toMyAddressPage(HttpServletRequest request, Model model) {
		try {
			PageParam pageParam = super.getPageParam(request, 8);
			Long customerId = getCustomerId();
			if (null != customerId) {
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("customerId", customerId);
				PageBean pageBean = addressService.listDeliveryAddressPage(pageParam, paramMap);
				model.addAttribute("addressList", pageBean.getRecordList());
				model.addAttribute("pageBean", pageBean);
			}
			model.addAttribute("page", "address");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "pc/my/address/my_address";
	}

	/**
	 * 保存地址
	 * 
	 * @param request
	 * @param model
	 * @param address
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:46:54
	 */
	@RequestMapping("/saveAddress")
	public String saveAddress(HttpServletRequest request, Model model, DeliveryAddressEntity address) {
		try {
			address.setCompanyId(getCompanyId());
			if (null != address) {
				if (address.getIsDefault() == null) {
					address.setIsDefault(0);
				}
				address.setCustomerId(getCustomerId());
				address.setCompanyId(getCompanyId());
				if (null != address.getId()) {
					address.setModifier(getUserName());
					addressService.updateDeliveryAddress(address);
				} else {
					address.setCreater(getUserName());
					address.setModifier(getUserName());
					addressService.saveDeliveryAddress(address);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/pc/my/address";
	}

	/**
	 * 获取地址信息
	 * 
	 * @param request
	 * @param addressId
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年12月5日 上午11:54:30
	 */
	@RequestMapping("/getAddressById")
	@ResponseBody
	public BaseReturn getAddressById(HttpServletRequest request, String addressId) {
		BaseReturn result = new BaseReturn();
		try {
			if (StringUtil.isNotNull(addressId)) {
				Long addressIdLong = Long.parseLong(addressId);
				DeliveryAddressEntity address = addressService.getDeliveryAddressById(addressIdLong);
				result.setData(address);
				result.setCode(SysCode.SUCCESS);
				result.setMsg("设置成功~");
			} else {
				result.setCode(SysCode.FAILED);
				result.setMsg("获取地址信息失败~");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("获取地址信息异常~");
		}
		return result;
	}

	/**
	 * 重设默认地址
	 * 
	 * @param request
	 * @param addressId
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:37:00
	 */
	@RequestMapping("/doSetDefault")
	@ResponseBody
	public BaseReturn doSetDefault(HttpServletRequest request, String addressId) {
		BaseReturn result = new BaseReturn();
		try {
			if (StringUtil.isNotNull(addressId)) {
				Long addressIdLong = Long.parseLong(addressId);
				// // 获取用户当前的默认地址
				// Map<String, Object> param = new HashMap<String, Object>();
				// param.put("customerId", getCustomerId());
				// param.put("isDefault", IsYesNo.YES);
				// List<DeliveryAddressEntity> addressList =
				// deliveryAddressService.listDeliveryAddress(param);
				// if (null != addressList && addressList.size() > 0) {
				// for (DeliveryAddressEntity address : addressList) {
				// address.setIsDefault(Integer.parseInt(IsYesNo.NO));
				// address.setModifier(getUserName());
				// deliveryAddressService.updateDeliveryAddress(address);
				// }
				// }
				// 设置指定地址为默认地址
				DeliveryAddressEntity address = new DeliveryAddressEntity();
				address.setId(addressIdLong);
				address.setIsDefault(Integer.parseInt(IsYesNo.YES));
				address.setCustomerId(getCustomerId());
				address.setModifier(getUserName());
				int rows = addressService.setDefaultAddress(address);
				if (rows > 0) {
					result.setCode(SysCode.SUCCESS);
					result.setMsg("设置成功~");
				} else {
					result.setCode(SysCode.ERROR);
					result.setMsg("设置失败，请重试！");
				}
			} else {
				result.setCode(SysCode.ERROR);
				result.setMsg("设置失败，请重试！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("设置失败，请重试！");
		}
		return result;
	}

	/**
	 * 删除地址
	 * 
	 * @param request
	 * @param model
	 * @param addressId
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:30:34
	 */
	@RequestMapping("/doDelAddress")
	@ResponseBody
	public BaseReturn doDelAddress(HttpServletRequest request, String addressId) {
		BaseReturn result = new BaseReturn();
		try {
			int rows = addressService.deleteDeliveryAddressById(Long.parseLong(addressId));
			if (rows > 0) {
				result.setCode(SysCode.SUCCESS);
				result.setMsg("删除成功~");
			} else {
				result.setCode(SysCode.ERROR);
				result.setMsg("删除失败，请重试！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("删除失败，请重试！");
		}
		return result;
	}
}
